package Server;

import Communication.TaskForModule;
import Communication.TaskForServer;
import ServerThreads.*;
import LiftEngine.Lift;
import java.util.LinkedList;

/**
 *
 * Class description goes here.
 *
 * @author Kamil Gajowy
 */
public class ServerController {

    private int port;

    private Lift lift;
    private ClientList myQueues;
    private ModuleList myModules;
    private MyThread[] serverThreads;
    private LinkedList<TaskForServer> taskList;
    private LinkedList<TaskForModule> taskMList;

    /**
    * Default constructor.
    */
    public ServerController(){
        System.out.println("initializing ServerController...");
        this.myQueues = new ClientList();
        this.myModules = new ModuleList();
        this.lift = new Lift();
        this.taskList = new LinkedList<TaskForServer>();
        this.taskMList = new LinkedList<TaskForModule>();

        SListener serverListenerThread =
                new SListener("ServerListener_Clients",
                myQueues, lift, taskMList);
        SListenerModule serverListenerModuleThread =
                new SListenerModule("ServerListener_Modules",
                myQueues, myModules, lift,taskList, taskMList);
        LiftOrganiser liftController =
                new LiftOrganiser("Lift Controller",
                myQueues, lift, taskList, taskMList);

        serverThreads = new MyThread[7];
        serverThreads[0] = serverListenerThread;
        serverThreads[1] = serverListenerModuleThread;
        serverThreads[6] = liftController;
    }

    public void startServer() {
        System.out.println("Starting Threads...");
        int i;
        for ( i = 0 ; i < serverThreads.length ; ++i ){
            if ( serverThreads[i] != null ){
                serverThreads[i].start();
                System.out.println("\t\t [ "
                        + serverThreads[i].getName() + " ]");
            }
        }

    }//end startServer

    void update() {
        
    }

    public ClientList getQueues() {
        return this.myQueues;
    }

    public void closeThreads() {
        System.out.println("Closing Threads...");
        int i;
        for ( i = 0 ; i < serverThreads.length ; ++i ){
            if ( serverThreads[i] != null ){
                serverThreads[i].stopMe();
            }
        }
    }

    /**
     * @return the lift
     */
    public Lift getLift() {
        return lift;
    }


}//end class
